
import matplotlib
matplotlib.use('WXAgg')
from fractions import Fraction
import numpy

from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
from matplotlib.figure import Figure


import wx

class SampleViewer(wx.Panel):

    def __init__(self, parent, model):
        wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
        self.parent = parent
        self.model = model

        self.figure1 = Figure()
        self.axes1 = self.figure1.add_subplot(111)
        self.canvas1 = FigureCanvas(self, wx.ID_ANY, self.figure1)

        self.figure2 = Figure()
        self.axes2 = self.figure2.add_subplot(111)
        self.canvas2 = FigureCanvas(self, wx.ID_ANY, self.figure2)

        self.figure3 = Figure()
        self.axes3 = self.figure3.add_subplot(111)
        self.canvas3 = FigureCanvas(self, wx.ID_ANY, self.figure3)

        self.figure4 = Figure()
        self.axes4 = self.figure4.add_subplot(111)
        self.canvas4 = FigureCanvas(self, wx.ID_ANY, self.figure4)

        self.figure5 = Figure()
        self.axes5 = self.figure5.add_subplot(111)
        self.canvas5 = FigureCanvas(self, wx.ID_ANY, self.figure5)

        self.figure6 = Figure()
        self.axes6 = self.figure6.add_subplot(111)
        self.canvas6 = FigureCanvas(self, wx.ID_ANY, self.figure6)

        sizer = wx.BoxSizer(wx.VERTICAL)

        sizerRow1 = wx.BoxSizer(wx.HORIZONTAL)
        sizerRow2 = wx.BoxSizer(wx.HORIZONTAL)
        sizerSample1 = wx.BoxSizer(wx.VERTICAL)
        sizerSample2 = wx.BoxSizer(wx.VERTICAL)
        sizerSample3 = wx.BoxSizer(wx.VERTICAL)
        sizerSample4 = wx.BoxSizer(wx.VERTICAL)
        sizerSample5 = wx.BoxSizer(wx.VERTICAL)
        sizerSample6 = wx.BoxSizer(wx.VERTICAL)
        self.option1 = wx.ComboBox(self, wx.ID_ANY, pos=(50, 170), size=(150, -1), choices=[], style=wx.CB_READONLY)
        self.option1.Bind(wx.EVT_COMBOBOX, self.updateFigure1)
        self.option2 = wx.ComboBox(self, wx.ID_ANY, pos=(50, 170), size=(150, -1), choices=[], style=wx.CB_READONLY)
        self.option2.Bind(wx.EVT_COMBOBOX, self.updateFigure2)
        self.option3 = wx.ComboBox(self, wx.ID_ANY, pos=(50, 170), size=(150, -1), choices=[], style=wx.CB_READONLY)
        self.option3.Bind(wx.EVT_COMBOBOX, self.updateFigure3)
        self.option4 = wx.ComboBox(self, wx.ID_ANY, pos=(50, 170), size=(150, -1), choices=[], style=wx.CB_READONLY)
        self.option4.Bind(wx.EVT_COMBOBOX, self.updateFigure4)
        self.option5 = wx.ComboBox(self, wx.ID_ANY, pos=(50, 170), size=(150, -1), choices=[], style=wx.CB_READONLY)
        self.option5.Bind(wx.EVT_COMBOBOX, self.updateFigure5)
        self.option6 = wx.ComboBox(self, wx.ID_ANY, pos=(50, 170), size=(150, -1), choices=[], style=wx.CB_READONLY)
        self.option6.Bind(wx.EVT_COMBOBOX, self.updateFigure6)
        
        sizerSample1.Add(self.canvas1, 0, wx.ALL, 5)
        sizerSample1.Add(self.option1, 0, wx.ALL, 5)
        sizerSample2.Add(self.canvas2, 0, wx.ALL, 5)
        sizerSample2.Add(self.option2, 0, wx.ALL, 5)
        sizerSample3.Add(self.canvas3, 0, wx.ALL, 5)
        sizerSample3.Add(self.option3, 0, wx.ALL, 5)
        sizerSample4.Add(self.canvas4, 0, wx.ALL, 5)
        sizerSample4.Add(self.option4, 0, wx.ALL, 5)
        sizerSample5.Add(self.canvas5, 0, wx.ALL, 5)
        sizerSample5.Add(self.option5, 0, wx.ALL, 5)
        sizerSample6.Add(self.canvas6, 0, wx.ALL, 5)
        sizerSample6.Add(self.option6, 0, wx.ALL, 5)

        sizerRow1.Add(sizerSample1)
        sizerRow1.Add(sizerSample2)
        sizerRow1.Add(sizerSample3)
        sizerRow2.Add(sizerSample4)
        sizerRow2.Add(sizerSample5)
        sizerRow2.Add(sizerSample6)
        
        sizer.Add(sizerRow1, 0, wx.ALL, 5)
        sizer.Add(sizerRow2, 0, wx.ALL, 5)

        self._resizeflag = False
        self._SetSize()

        self.SetSizer(sizer)
        self.Fit()

    def updateFigure1(self, event):
        self.updateFigure(self.option1.GetValue(), self.axes1, self.figure1)

    def updateFigure2(self, event):
        self.updateFigure(self.option2.GetValue(), self.axes2, self.figure2)

    def updateFigure3(self, event):
        self.updateFigure(self.option3.GetValue(), self.axes3, self.figure3)

    def updateFigure4(self, event):
        self.updateFigure(self.option4.GetValue(), self.axes4, self.figure4)

    def updateFigure5(self, event):
        self.updateFigure(self.option5.GetValue(), self.axes5, self.figure5)

    def updateFigure6(self, event):
        self.updateFigure(self.option6.GetValue(), self.axes6, self.figure6)

    def updateFigure(self, filename, axes, figure):
        location = self.model.getFile(filename)
        audio = self.model.getAudio(location)
        axes.clear()
        x = numpy.array(range(0, len(audio[1])))/44100.0
        axes.plot(x, audio[1]/(2.0**15))
        figure.canvas.draw()


    def _onSize( self, event ):
        self._resizeflag = True

    def _SetSize( self ):
        pixels = tuple( self.parent.GetClientSize() )
        self.SetSize( pixels )
        self.canvas1.SetSize((400, 275))
        self.canvas2.SetSize((400, 275))
        self.canvas3.SetSize((400, 275))
        self.canvas4.SetSize((400, 275))
        self.canvas5.SetSize((400, 275))
        self.canvas6.SetSize((400, 275))

    def update(self):
        self.updateCombo(self.option1)
        self.updateCombo(self.option2)
        self.updateCombo(self.option3)
        self.updateCombo(self.option4)
        self.updateCombo(self.option5)
        self.updateCombo(self.option6)

    def updateCombo(self, combo):
        combo.Clear()
        filenames = self.model.getFilenames()
        for filename in filenames:
            combo.Append(filename.keys()[0])
